package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.util.AssertUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;

import java.util.List;
import java.util.Map;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService<FlightActivityDaily> {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public FlightActivityDaily add(FlightActivityDaily flightActivityDaily) {
        validFlightActivityDaily(flightActivityDaily);
        AssertUtil.isNull(flightActivityDaily.getId(), "实体id不能填写");

        int insert = flightActivityDailyMapper.insert(flightActivityDaily);
        if (insert > 0) {
            return flightActivityDaily;
        }
        throw new RuntimeException("新增实体失败");
    }

    /**
     * 通用验证实体信息
     */
    private void validFlightActivityDaily(FlightActivityDaily flightActivityDaily) {
        if (flightActivityDaily == null) {
            throw new RuntimeException("实体信息不能为空");
        }
        AssertUtil.maxLength(flightActivityDaily.getIcaoCode(), 6, "ICAO码不能超过6位");
        AssertUtil.notBlank(flightActivityDaily.getIcaoCode(), "ICAO码不能为空");
    }

    @Override
    public String delete(String flightActivityDailyId) {
        if (StrUtil.isBlank(flightActivityDailyId)) {
            throw new RuntimeException("实体id不能为空");
        }
        int delete = flightActivityDailyMapper.deleteById(flightActivityDailyId);
        if (delete > 0) {
            return flightActivityDailyId;
        }
        throw new RuntimeException("删除实体失败");
    }

    @Override
    public FlightActivityDaily edit(FlightActivityDaily edit) {
        validFlightActivityDaily(edit);
        String flightActivityDailyId = edit.getId();
        if (StrUtil.isBlank(flightActivityDailyId)) {
            throw new RuntimeException("实体id不能为空");
        }
        FlightActivityDaily one = findOne(flightActivityDailyId);
        if (one == null) {
            throw new RuntimeException("实体不存在");
        }
        int update = flightActivityDailyMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改实体失败");
    }

    @Override
    public FlightActivityDaily findOne(String flightActivityDailyId) {
        if (StrUtil.isBlank(flightActivityDailyId)) {
            throw new RuntimeException("实体id不能为空");
        }
        return flightActivityDailyMapper.selectById(flightActivityDailyId);
    }

    @Override
    public PageVo<FlightActivityDaily> findPage(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(oderByField));
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(oderByField));
            }
        }
        // 支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        // 查询icao编码
        if (StrUtil.isNotBlank(icao)) {
            queryWrapper.eq("icao_code", icao);
        }
        // 查询时间范围
        if (StrUtil.isNotBlank(startDate)) {
            queryWrapper.ge("date", startDate);
        }
        if (StrUtil.isNotBlank(endDate)) {
            queryWrapper.le("date", endDate);
        }
        Page<FlightActivityDaily> pageParam = new Page<>(page, pageSize);
        Page<FlightActivityDaily> flightActivityDailyPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(flightActivityDailyPage);
    }

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        // to//do 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date", date);
        return flightActivityDailyMapper.selectList(queryWrapper);
    }

    @Override
    public FlightActivityDaily findOneByIcaoAndDate(String icao, String date) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("icao_code", icao);
        queryWrapper.eq("date", date);
        return flightActivityDailyMapper.selectOne(queryWrapper);
    }

    /**
     * 更新补充飞机信息 registration,airline,aircraft_type 三个字段
     *
     * @param entity 实体信息
     */
    @Override
    public void fillDictForAirCraftInfo(FlightActivityDaily entity) {
        String icaoCode = entity.getIcaoCode();
        if (StrUtil.isNotBlank(icaoCode)) {
            Map<String, String> find = flightActivityDailyMapper.findDictForAirCraftInfo(icaoCode);
            if (find != null) {
                // 找到飞机字典信息, 补充字段
                log.info("找到飞机字典信息, 补充字段, icao: {}", icaoCode);
                String registration = find.get("registration");
                String airline = find.get("airline");
                String aircraft_type = find.get("aircraft_type");
                entity.setRegistration(registration);
                entity.setAirline(airline);
                entity.setAircraftType(aircraft_type);
            } else {
                log.info("未找到飞机字典信息, icao: {}", icaoCode);
            }
        }
    }

}
